package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.financing.platform.service.IFpFundAwkwardnessService;
import com.financing.platform.service.IFpFundInfoService;
import com.financing.platform.service.IFpProjectTransferHisService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpFundAwkwardnessDTO;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.FpProjectTransferHisDTO;
import com.financing.platform.dto.UserInfoDTO;

/**
 * @Description: Controller
 * @author caojiayao
 * @version 1.0
 * @created 
 */
@Controller
@RequestMapping("/api/fund")
@Scope("prototype")
public class FpFundAwkwardnessAction extends BaseAction {
	@Autowired
	private IFpFundAwkwardnessService fpFundAwkwardnessService;
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	@Autowired
	private IFpProjectTransferHisService fpProjectTransferHisService;
	
	@RequestMapping("fdAwkwardnessQueryList.shtml")
	@ResponseBody
	public ResultMap queryList(HttpServletRequest request,
			HttpServletResponse response,FpFundAwkwardnessDTO dto,Pager pager) {
		try{
			log.info("Begin query fdAwkwardnessQueryList!");
			
			dto.setBegin(pager.getStartIndex());
			dto.setPageSize(pager.getPageSize());
			
			int count = fpFundAwkwardnessService.countFpFundAwkwardness(dto);
			pager.setTotalCount(count);
			List<FpFundAwkwardnessDTO> list = fpFundAwkwardnessService.listFpFundAwkwardness(dto);
			
			//setCreateBy  修改成转移人
			for (FpFundAwkwardnessDTO object:list) {
				if(object !=null){
					//获取转移后的 权限人
					FpFundInfoDTO bean = new FpFundInfoDTO();
					bean.setJjbm(object.getJjbm());
					bean = fpFundInfoService.getFpFundInfo(bean);
					if(bean !=null){
						FpProjectTransferHisDTO tfdto = new FpProjectTransferHisDTO();
						tfdto.setPProjectId(bean.getId());
						tfdto.setpProjectType("02");//“02：私募” 
						String createBy = fpProjectTransferHisService.getTransferUserNameByProjectID(tfdto);
						if(!StringUtil.isNullOrEmptyStr(createBy)){
							object.setCreateBy(createBy);
						}
					}
				}
			}
			
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End query fdAwkwardnessQueryList!");
		}catch(Exception e){
			log.error("fdAwkwardnessQueryList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("fdAwkwardnessAdd.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response,FpFundAwkwardnessDTO dto) {
		try{
			log.info("Begin add fdAwkwardnessAdd!");
			//判断是否有权限修改产品
			if(!isAccess(dto.getJjbm(),request)){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_NO_ACCESS);
				return resultMap;
			}
			DTOdateSet.setCreateInfo(dto, request.getSession());
			
			Integer id =fpFundAwkwardnessService.insertFpFundAwkwardness(dto);
			
			resultMap.getAttribute().put("id",id);
			
			if(id !=null){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End add fdAwkwardnessAdd !");
		}catch(Exception e){
			log.error("fdAwkwardnessAdd error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	
	@RequestMapping("fdAwkwardnessUpdate.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,FpFundAwkwardnessDTO dto) {
		try{
			log.info("Begin fdAwkwardnessUpdate !");
			//判断是否有权限修改产品
			if(!isAccess(dto.getJjbm(),request)){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_NO_ACCESS);
				return resultMap;
			}
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			
			int count = fpFundAwkwardnessService.updateFpFundAwkwardness(dto);
			
			resultMap.getAttribute().put("update count",count);
			if(count >0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End fdAwkwardnessUpdate !");
		}catch(Exception e){
			log.error("fdAwkwardnessUpdate error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	@RequestMapping("fdAwkwardnessDelete.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request,
			HttpServletResponse response,FpFundAwkwardnessDTO dto) {
		try{
			log.info("Begin delete fdAwkwardnessDelete !");
			
			
			FpFundAwkwardnessDTO bean = fpFundAwkwardnessService.getFpFundAwkwardness(dto.getId());
			
			if(bean == null){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_RESULTDATA_NOTEXIST);
				return resultMap;
			}
			
			//判断是否有权限修改产品
			if(!isAccess(bean.getJjbm(),request)){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_NO_ACCESS);
				return resultMap;
			}
			int count = fpFundAwkwardnessService.delFpFundAwkwardness(dto.getId());
			
			resultMap.getAttribute().put("delete count",count);
			
			if(count > 0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End delete fdAwkwardnessDelete !");
		}catch(Exception e){
			log.error("delete fdAwkwardnessDelete!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	
	@RequestMapping("fdAwkwardnessDetail.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request,
			HttpServletResponse response, FpFundAwkwardnessDTO requestDto) {
		try{
			FpFundAwkwardnessDTO dto = fpFundAwkwardnessService.getFpFundAwkwardness(requestDto.getId());
			if(dto !=null){
				//获取转移后的 权限人
				FpFundInfoDTO bean = new FpFundInfoDTO();
				bean.setJjbm(dto.getJjbm());
				bean = fpFundInfoService.getFpFundInfo(bean);
				if(bean !=null){
					FpProjectTransferHisDTO tfdto = new FpProjectTransferHisDTO();
					tfdto.setPProjectId(bean.getId());
					tfdto.setpProjectType("02");//“02：私募” 
					String createBy = fpProjectTransferHisService.getTransferUserNameByProjectID(tfdto);
					if(!StringUtil.isNullOrEmptyStr(createBy)){
						dto.setCreateBy(createBy);
					}
				}
			}
			
			resultMap.setAttribute("detail", dto);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("fdAwkwardnessDetail error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;

	}
	/**
	 * 判断要修改的私募产品是否为当前用户所有
	 * @param projectId
	 * @param request
	 * @return
	 */
	private boolean isAccess(String jjbm,HttpServletRequest request){
		
		log.info("Begin isAccess jjbm"+jjbm);
		
		UserInfoDTO user = (UserInfoDTO) request.getSession().getAttribute(DTOdateSet.user);
		
		//超级管理员具有所有产品的管理权限
		if(user.getUsername().equals("admin")){
			return true;
		}
		//权限扭转接收人
		FpFundInfoDTO bean = new FpFundInfoDTO();
		bean.setJjbm(jjbm);
		bean = fpFundInfoService.getFpFundInfo(bean);
		if(bean !=null){
			FpProjectTransferHisDTO dto = new FpProjectTransferHisDTO();
			dto.setPProjectId(bean.getId());
			dto.setpProjectType("02");//“02：私募”
			String createBy = fpProjectTransferHisService.getTransferUserNameByProjectID(dto);
			if(!StringUtil.isNullOrEmptyStr(createBy)){
				//是否是扭转接收人
				if(user.getUsername().equals(createBy)){
					return true;
				}else{
					return false;
				}
			}
		}
		 if(bean!=null && bean.getCreateBy().equals(user.getUsername())){
			 return true;
		 }
		return false;
	}
}